package com.mamahao.vmnetframework.framework.event

import android.app.Activity
import android.widget.Toast
import com.mamahao.vmnetframework.framework.view.hideProgressBar
import com.mamahao.vmnetframework.framework.view.showProgressBar

class ActionTodo{

    companion object {

        @JvmStatic
        fun actionMessageTodo(activity: Activity?, baseActionEvent: BaseActionEvent?, block: ((BaseActionEvent?) -> Boolean)? = null){
            block?.takeUnless {
                it.invoke(baseActionEvent)
            } .also {
                when(baseActionEvent?.mAation){
                    BaseActionEvent.SHOW_LOADING_DIALOG ->{
                        activity?.showProgressBar()
                    }
                    BaseActionEvent.DISMISS_LOADING_DIALOG ->{
                        activity?.hideProgressBar()
                    }
                    BaseActionEvent.SHOW_TOAST ->{
                        baseActionEvent.takeIf {
                            it.message?.isNotEmpty() ?: false
                        } ?.also {
                            Toast.makeText(activity, it.message, Toast.LENGTH_SHORT).show()
                        }
                    }
                    BaseActionEvent.FINISH ->{
                        activity?.finish()
                    }
                    BaseActionEvent.FINISH_WITH_RESULT_OK ->{
                        activity?.setResult(Activity.RESULT_OK)
                        activity?.finish()
                    }
                    else -> {
                        block?.invoke(baseActionEvent)
                    }
                }
            }
        }
    }
}